<?php

/**
 * This is the model class for table "{{pro_tuan}}".
 *
 * The followings are the available columns in table '{{pro_tuan}}':
 * @property integer $zpt_id
 * @property integer $zpt_type
 * @property string $zpt_title
 * @property integer $zpt_start_time
 * @property integer $zpt_end_time
 * @property integer $zpt_nums
 * @property integer $zpt_limit_nums
 * @property integer $zpt_salenums
 * @property integer $zpt_min_nums
 * @property string $zpt_content
 * @property string $zmg_id
 * @property integer $zpt_close
 * @property double $zpt_saleprice
 * @property double $zpt_price
 * @property integer $zp_id
 * @property integer $zpt_status
 * @property string $zpt_pic
 * @property integer $zpt_hist
 * @property integer $zpt_listorder
 * @property integer $inputtime
 * @property integer $updatetime
 */
class ProTuan extends CActiveRecord
{
	//店铺名称，用于筛选
	public $company_name;
	
	//状态，用于筛选
	public $status;
	
	//行业一级分类 ，用于前台筛选
	public $category_top;
	
	//价格区间，用于前台筛选
	public $priceInterval;
	
	//排序方式
	public $list_order;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ProTuan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pro_tuan}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zpt_type, zpt_nums, zpt_limit_nums, zpt_salenums, category_top, zpt_min_nums, status, list_order, priceInterval, zpt_close, zp_id, zm_id, zpt_status, zpt_hist, zpt_listorder, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zpt_saleprice, zpt_price, zpt_discount', 'numerical'),
			array('zpt_title, zmg_id, zpt_pic, zpt_start_time, zpt_end_time, zpt_nostatus, company_name', 'length', 'max'=>255),
			array('zpt_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpt_id, zpt_type, zpt_title, zpt_start_time, zpt_end_time, zpt_nums, zpt_limit_nums, zpt_nostatus, zpt_salenums, zpt_min_nums, zpt_content, zmg_id, zpt_close, zpt_saleprice, zpt_price, zp_id, zm_id, zpt_status, zpt_pic, zpt_hist, zpt_listorder, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'product'=>array(self::BELONGS_TO, 'Product', 'zp_id'),
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpt_id' => 'Zpt',
			'zpt_type' => 'Zpt Type',
			'zpt_title' => 'Zpt Title',
			'zpt_start_time' => 'Zpt Start Time',
			'zpt_end_time' => 'Zpt End Time',
			'zpt_nums' => 'Zpt Nums',
			'zpt_limit_nums' => 'Zpt Limit Nums',
			'zpt_salenums' => 'Zpt Salenums',
			'zpt_min_nums' => 'Zpt Min Nums',
			'zpt_content' => 'Zpt Content',
			'zmg_id' => 'Zmg',
			'zpt_close' => 'Zpt Close',
			'zpt_saleprice' => 'Zpt Saleprice',
			'zpt_price' => 'Zpt Price',
			'zpt_discount' => 'Zpt Discount',
			'zp_id' => 'Zp',
			'zm_id' => 'Zm',
			'zpt_status' => 'Zpt Status',
			'zpt_nostatus' => 'Zpt Nostatus',
			'zpt_pic' => 'Zpt Pic',
			'zpt_hist' => 'Zpt Hist',
			'zpt_listorder' => 'Zpt Listorder',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($type=10)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpt_id',$this->zpt_id);
		$criteria->compare('zpt_type',$this->zpt_type);
		$criteria->compare('zpt_title',$this->zpt_title,true);
		$criteria->compare('zpt_start_time',$this->zpt_start_time);
		$criteria->compare('zpt_end_time',$this->zpt_end_time);
		$criteria->compare('zpt_nums',$this->zpt_nums);
		$criteria->compare('zpt_limit_nums',$this->zpt_limit_nums);
		$criteria->compare('zpt_salenums',$this->zpt_salenums);
		$criteria->compare('zpt_min_nums',$this->zpt_min_nums);
		$criteria->compare('zpt_content',$this->zpt_content,true);
		$criteria->compare('zmg_id',$this->zmg_id,true);
		$criteria->compare('zpt_close',$this->zpt_close);
		$criteria->compare('zpt_discount',$this->zpt_discount);
		$criteria->compare('zpt_price',$this->zpt_price);
		$criteria->compare('zpt_nostatus',$this->zpt_nostatus);
		$criteria->compare('zpt_pic',$this->zpt_pic,true);
		$criteria->compare('zpt_hist',$this->zpt_hist);
		$criteria->compare('zpt_listorder',$this->zpt_listorder);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		$criteria->join = '';
		//根据公司名称筛选
		if(!empty($this->company_name)){
			$criteria->join .= ' LEFT JOIN {{Company}} c ON c.zm_id=t.zm_id ';
			$criteria->addCondition(' c.zc_name like "%'.$this->company_name.'%" ');
		}else{
			$criteria->compare('zm_id',$this->zm_id);
		}
		
		//根据状态查询
		if(!empty($this->status)){
			if($this->status==1){
				$criteria->compare('zpt_start_time','<='.time());
				$criteria->compare('zpt_end_time','>='.time());
				$criteria->compare('zpt_status',$this->zpt_status);
			}elseif($this->status==2){
				$criteria->compare('zpt_start_time','>'.time());
				$criteria->compare('zpt_status',$this->zpt_status);
			}elseif($this->status==3){
				$criteria->compare('zpt_end_time','<'.time());
				$criteria->compare('zpt_status',$this->zpt_status);
			}elseif($this->status==4){//待审核的团购
				$criteria->compare('zpt_start_time',$this->zpt_start_time);
				$criteria->compare('zpt_end_time',$this->zpt_end_time);
				$criteria->compare('zpt_status',1);
			}elseif($this->status==5){//未开始和进行中的团购
				$criteria->compare('zpt_end_time','>'.time());
				$criteria->compare('zpt_status',2);
			}
		}else{
			$criteria->compare('zpt_start_time',$this->zpt_start_time);
			$criteria->compare('zpt_end_time',$this->zpt_end_time);
			$criteria->compare('zpt_status',$this->zpt_status);
		}
		
		//行业一级分类筛选，用于前台
		if(!empty($this->category_top)){
			$criteria->join .= ' LEFT JOIN {{product}} p ON p.zp_id=t.zp_id ';
			$criteria->join .= ' LEFT JOIN {{category}} zca ON zca.zca_id=p.zca_id ';
			$criteria->addCondition(" (zca.zca_id={$this->category_top} OR zca.zca_parentid={$this->category_top} OR zca.zca_topid={$this->category_top})");
		}else{
			$criteria->compare('zp_id',$this->zp_id);
		}
		
		//根据价格区间筛选
		if(!empty($this->priceInterval)){
			if($this->priceInterval==1){
				$criteria->compare('zpt_price','<10');//10元以下
			}elseif($this->priceInterval==2){
				$criteria->addBetweenCondition('zpt_price',10,50);//10元-50元
			}elseif($this->priceInterval==3){
				$criteria->addBetweenCondition('zpt_price',50,100);//50元-100元
			}elseif($this->priceInterval==4){
				$criteria->addBetweenCondition('zpt_price',100,300);//100元-300元
			}elseif($this->priceInterval==5){
				$criteria->addBetweenCondition('zpt_price',300,500);//300元-500元
			}elseif($this->priceInterval==6){
				$criteria->compare('zpt_price','>500');//500以上
			}
		}else{
			$criteria->compare('zpt_start_time',$this->zpt_saleprice);
		}
		
		//排序方式
		if(!empty($this->list_order)){
			if($this->list_order==1){
				$criteria->order = 'zpt_price ASC';//价格升序
			}elseif($this->list_order==2){
				$criteria->order = 'zpt_price DESC';//价格降序
			}elseif($this->list_order==3){
				$criteria->order = 'zpt_salenums ASC';//销量升序
			}elseif($this->list_order==4){
				$criteria->order = 'zpt_salenums DESC';//销量降序
			}elseif($this->list_order==5){
				$criteria->order = 'zpt_discount ASC';//折扣升序
			}
		}else{
			$criteria->order = 'inputtime DESC';//默认排序 开始时间升序
		}
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>$type,
					),
		));
	}
	
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
			$this->zm_id = Company::model()->getCmpUserId();
			//如果为团购，则判断是否要审核，为抢购秒杀则不能审核
			if($this->zpt_type==1){
				//后台发布不需要审核
				if($this->zm_id==1){
					$this->zpt_status = 2;//审核状态
				}else{
					$this->zpt_status = 1;//审核状态
				}
			}elseif($this->zpt_type==2){
				$this->zpt_status = 2;//审核状态为通过，则不能审核
			}
		}else{
			$this->updatetime = time();
		}
		$this->zpt_discount = number_format($this->zpt_price/$this->zpt_saleprice*10,1,'.','');
		return true;
	}
	
	/**
	 * 状态，用于筛选
	 */
	public function getStatus($type=null){
		if($type==null){
			return array(
					''=>'全部',
					'1'=>'正在进行',
					'2'=>'未开始',
					'3'=>'已结束',
			);
		}
	}
	
	/**
	 * 状态，用于团购筛选
	 */
	public function getTuanStatus($type=null){
		if($type==null){
			return array(
					''=>'全部',
					'4'=>'待审核',
					'1'=>'正在进行',
					'2'=>'未开始',
					'3'=>'已结束',
			);
		}
	}
	
	/**
	 * 获取分类统计
	 * 获得行业一级分类，以级分类下的团购产品数量，数据缓存1天
	 */
	public function getCateStat(){
		$data = Yii::app()->filecache->get('tuanCateStat');//获取缓存
		if(!empty($data)){
			return $data;
		}
		$criteria=new CDbCriteria;
		$criteria->compare('zca_level','1');
		$criteria->order = 'zca_listorder ASC';
		$cateTop = Category::model()->findAll($criteria);
		
		$criteria=new CDbCriteria;
		$criteria->compare('zpt_status',2);//审核状态为通过
		$criteria->compare('zpt_type',1);//类型为团购
		$criteria->compare('zpt_end_time','>'.time());//结束时间大于当前时间
		$criteria->compare('zpt_close',1);//是否关闭1否2是3删除
		$data['']['title'] = '全部';
		$data['']['stat'] = count($this->findAll($criteria));
		
		foreach($cateTop as $val){
			$criteria=new CDbCriteria;
			$criteria->compare('zpt_status',2);//审核状态为通过
			$criteria->compare('zpt_type',1);//类型为团购
			$criteria->compare('zpt_end_time','>'.time());//结束时间大于当前时间
			$criteria->compare('zpt_close',1);//是否关闭1否2是3删除
			//分类下的产品
			$criteria->join = ' LEFT JOIN {{product}} p ON p.zp_id=t.zp_id ';
			$criteria->join .= ' LEFT JOIN {{category}} zca ON zca.zca_id=p.zca_id ';
			$criteria->addCondition(" (zca.zca_id={$val->zca_id} OR zca.zca_parentid={$val->zca_id} OR zca.zca_topid={$val->zca_id})");
				
			$data[$val->zca_id]['title'] = $val->zca_title;
			$data[$val->zca_id]['stat'] = count($this->findAll($criteria));
		}
		//设置缓存
		Yii::app()->filecache->set('tuanCateStat',$data,3600*24);
		return $data;
	}
	
	/**
	 * 获取价格统计
	 * 获得行业一级分类，以级分类下的团购产品数量，数据缓存1天
	 */
	public function getPriceStat(){
		$data = Yii::app()->filecache->get('tuanPriceStat');//获取缓存
		if(!empty($data)){
			return $data;
		}
		$priceInterval = array(
				'1'=>'10元以下',
				'2'=>'10元-50元',
				'3'=>'50元-100元',
				'4'=>'100元-300元',
				'5'=>'300元-500元',
				'6'=>'500元以上',
				);
		$criteria=new CDbCriteria;
		$criteria->compare('zpt_status',2);//审核状态为通过
		$criteria->compare('zpt_type',1);//类型为团购
		$criteria->compare('zpt_end_time','>'.time());//结束时间大于当前时间
		$criteria->compare('zpt_close',1);//是否关闭1否2是3删除
		$data['']['title'] = '全部';
		$data['']['stat'] = count($this->findAll($criteria));
		foreach($priceInterval as $key=>$val){
			$criteria=new CDbCriteria;
			$criteria->compare('zpt_status',2);//审核状态为通过
			$criteria->compare('zpt_type',1);//类型为团购
			$criteria->compare('zpt_end_time','>'.time());//结束时间大于当前时间
			$criteria->compare('zpt_close',1);//是否关闭1否2是3删除
			//价格区间
			if($key==1){
				$criteria->compare('zpt_price','<10');//10元以下
			}elseif($key==2){
				$criteria->addBetweenCondition('zpt_price',10,50);//10元-50元
			}elseif($key==3){
				$criteria->addBetweenCondition('zpt_price',50,100);//50元-100元
			}elseif($key==4){
				$criteria->addBetweenCondition('zpt_price',100,300);//100元-300元
			}elseif($key==5){
				$criteria->addBetweenCondition('zpt_price',300,500);//300元-500元
			}elseif($key==6){
				$criteria->compare('zpt_price','>500');//500以上
			}
			
			$data[$key]['title'] = $val;
			$data[$key]['stat'] = count($this->findAll($criteria));
		}
		//设置缓存
		Yii::app()->filecache->set('tuanPriceStat',$data,3600*24);
		return $data;
	}
	
	public function DelCatcache(){
		Yii::app()->filecache->delete('tuanCateStat');
		Yii::app()->filecache->delete('tuanPriceStat');
	}
	
	function afterSave(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
	
	function afterDelete(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
}